// Copyright Notice:
//				input_events.js v1.0.1
//			Copyright@2008-2009 - OpenSiteMobile
//				All rights reserved
// ==========================================================================
//			http://www.opensitemobile.com
// ==========================================================================
// Contact Information:
//			Author: Dwight Vietzke
//			Email:  dwight_vietzke@yahoo.com

/*
	OpenSiteMobile 'text input' selection/range functions
	and several 'iframe' related selection/range functions
	w/dojo integration
*/

dojo.provide("msos.input_events");
dojo.require("msos.common");


// --------------------------
// Iframe event helper functions
// --------------------------

msos.input_events.iframe_add_event = function(el, evname, func) {
	if (el.attachEvent)	{ el.attachEvent("on" + evname, func);		}
	else			{ el.addEventListener(evname, func, false);	}
}

msos.input_events.iframe_event_target = function(ev) {
	var targ = null;
	if	(ev.target)	targ = ev.target;
	else if	(ev.srcElement)	targ = ev.srcElement;

	if (!ev.target)		ev.target = targ;
	if (targ.nodeType == 3)	ev.target = targ.parentNode;
}


// --------------------------
// Input event bundling functions
// --------------------------

msos.input_events.set_event = function(tool_obj, input_array) {

	for(var j = 0; j < input_array.length; j++) {
		var temp_text = 'failed';
		if (/iframe/i.test(input_array[j].nodeName)) {
			var ifr_el = input_array[j];

			var input_mousedown = function(evt) {
				// Need this since we don't use dojo.connect
				if (dojo.isIE) evt = ifr_el.contentWindow.event;
				tool_obj.tool_target = null;
				tool_obj.tool_iframe = null;
				msos.common.event_debug(evt, "\nIframe mousedown");
				return true;
			}
			var input_mouseup = function(evt) {
				if (dojo.isIE) evt = ifr_el.contentWindow.event;
				msos.input_events.iframe_event_target(evt);
				tool_obj.tool_target = evt.target;
				tool_obj.tool_iframe = ifr_el;
				msos.common.event_debug(evt, "\nIframe mouseup :\ntool target -> " + evt.target.nodeName);
				return true;
			}
			var if_body = ifr_el.contentWindow.document.body;
			// Turn on editable functions (order is important - must be editable before adding events for Opera)
			if (dojo.isIE)  { ifr_el.contentWindow.document.body.contentEditable = true;    }
			else            { ifr_el.contentDocument.designMode = 'on';                     }

			msos.input_events.iframe_add_event(if_body, 'mousedown', input_mousedown);
			msos.input_events.iframe_add_event(if_body, 'mouseup',   input_mouseup);
			temp_text = 'added';
		} else if (/textarea|text|password/i.test(input_array[j].type)) {

			var input_mousedown = function(evt) {
				tool_obj.tool_target = null;
				tool_obj.tool_iframe = null;
				msos.common.event_debug(evt, "\nTA mousedown");
				return true;
			}
			var input_mouseup = function(evt) {
				tool_obj.tool_target = evt.target;
				tool_obj.tool_iframe = null;
				msos.common.event_debug(evt, "\nTA mouseup :\ntool target -> " + evt.target.nodeName);
				return true;
			}
			dojo.connect(input_array[j], "onmousedown", null, input_mousedown);
			dojo.connect(input_array[j], "onmouseup",   null, input_mouseup);
			temp_text = 'added';
		  }
		console.debug('set_event -> ' + input_array[j].nodeName + ', input_array[' + j + '] ' + temp_text);
	}
}